<style lang="less">
    @import "./login.less";
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="phone">
                            <Input v-model="form.phone" placeholder="请输入用户名">
                            <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                            <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <!-- <p class="login-tip">输入任意用户名和密码即可</p> -->
                </div>
            </Card>
        </div>
    </div>
</template>
<script>
    import Cookies from 'js-cookie';
    import config_api from '@/utils/config_api.js';
    import {set_local_user_info} from '@/utils/config_common.js';
    import {set_password} from '@/utils/config_common.js';
    import {updateRoles} from './access';

    export default {
        data () {
            return {
                form: {
                    phone: '',
                    password: ''
                },
                rules: {
                    phone: [{required: true, message: '账号不能为空', trigger: 'blur'}],
                    password: [{required: true, message: '密码不能为空', trigger: 'blur'}]
                }
            };
        },
        methods: {
            handleSubmit () {
                this.$refs.loginForm.validate(valid => {
                    if (valid) {
                        let opts = {
                            url: `${this.$hostUrl.cloud_host_v2}${config_api.login.url}`,
                            method: config_api.login.method
                        };
                        let params = {
                            phone: this.form.phone,
                            password: this.form.password,
                            sys_mark: 'proxy_bbc'
                        };
                        this.$axios({
                            method: opts.method,
                            url: opts.url,
                            data: params
                        })
                            .then(res => {
                                res = res.data;
                                if (res.status == 200) {
                                    set_local_user_info(res.data); //存储用户信息
                                    set_password(this.form.password);
                                    Cookies.set(`area_cloud_user`, this.form.phone);
                                    Cookies.set('area_user_role_tag', 1);
                                    this.$store.commit(
                                        'setAvator',
                                        require('@/images/logo.png')
                                    );
                                    sessionStorage.setItem('area_cloud_access', JSON.stringify([]));
                                    updateRoles();
                                    this.$router.push('/');
                                } else {
                                    this.$Message.error(res.message);
                                }
                            });
                    }
                });
            }
        }
    };
</script>

